package longrise.phone.com.bjjt_henan.main;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.compensate.IntentInsuranceReportView;
import longrise.phone.com.bjjt_henan.widget.PromptDialog;

/**
 * 服务中心详情
 */

public class ServiceCenterDetailView extends LFView implements View.OnClickListener {

    private Context mContext;

    private View mView;

    private ImageView addressIV;

    private ImageView phoneIV;

    private Button navBtn;

    private EntityBean dataBean;

    private TextView countTV;

    private Button backBtn;

    private String appcaseno;

    private EntityBean[] infoBeans;

    private String isSingle;

    public ServiceCenterDetailView(Context context, EntityBean bean, String appcaseno) {
        super(context);
        this.mContext = context;
        this.dataBean = bean;
        this.appcaseno = appcaseno;
    }

    public ServiceCenterDetailView(Context context, EntityBean bean, String appcaseno, EntityBean[] beans, String isSingle) {
        super(context);
        this.mContext = context;
        this.dataBean = bean;
        this.appcaseno = appcaseno;
        this.infoBeans = beans;
        this.isSingle = isSingle;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_servicecenter_detail, null);
        if (null == mView) {
            return;
        }
        //标题栏
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        ImageView iconIV = (ImageView) mView.findViewById(R.id.centerdetail_iv_img);
        TextView nameTV = (TextView) mView.findViewById(R.id.centerdetail_tv_name);
        TextView rateTV = (TextView) mView.findViewById(R.id.centerdetail_tv_rate);
        TextView phoneTV = (TextView) mView.findViewById(R.id.centerdetail_tv_phone);
        phoneIV = (ImageView) mView.findViewById(R.id.centerdetail_iv_phone);
        TextView addressTV = (TextView) mView.findViewById(R.id.centerdetail_tv_address);
        addressIV = (ImageView) mView.findViewById(R.id.centerdetail_iv_address);
        TextView infoTV = (TextView) mView.findViewById(R.id.centerdetail_tv_info);
        navBtn = (Button) mView.findViewById(R.id.centerdetail_btn_nav);
        countTV = (TextView) mView.findViewById(R.id.centerdetail_tv_count);
        backBtn = (Button) mView.findViewById(R.id.centerdetail_btn_back);

        setEvent(true);

        String url = dataBean.getString("picture");
        if (!TextUtils.isEmpty(url)) {
            ImageLoader.getInstance().displayImage(url, iconIV, CommontUtils.getOptions());
        }
        nameTV.setText(dataBean.getString("centername", ""));
        countTV.setText(dataBean.getString("number", "0") + "条评论");
        rateTV.setText(dataBean.getString("score", "0.0"));
        addressTV.setText(dataBean.getString("centeraddress", ""));
        phoneTV.setText(dataBean.getString("centerphone", ""));
        infoTV.setText(dataBean.getString("centernote", ""));
        infoTV.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    private void setEvent(boolean flag) {
        if (phoneIV != null) {
            phoneIV.setOnClickListener(flag ? this : null);
        }
        if (addressIV != null) {
            addressIV.setOnClickListener(flag ? this : null);
        }
        if (navBtn != null) {
            navBtn.setOnClickListener(flag ? this : null);
        }
        if (countTV != null) {
            countTV.setOnClickListener(flag ? this : null);
        }
        if (backBtn != null) {
            backBtn.setOnClickListener(flag ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.centerdetail_iv_phone:
                String phone = dataBean.getString("centerphone");
                if (TextUtils.isEmpty(phone)) {
                    UiUtil.showToast(mContext, "电话号码有误！");
                    return;
                }
                checkPhone(phone);
                break;
            case R.id.centerdetail_iv_address:
            case R.id.centerdetail_btn_nav:
                showReportDialog();
                break;
            case R.id.centerdetail_tv_count:
                CommentView cView = new CommentView(mContext, dataBean.getString("centerid"));
                FrameworkManager.getInstance().showNewForm(mContext, cView);
                break;
            case R.id.centerdetail_btn_back:
                bindServiceCenter();
                break;
            default:
                break;
        }
    }

    //拨号界面
    private void checkPhone(String phone) {
        Uri uri = Uri.parse("tel:" + phone);
        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
        mContext.startActivity(intent);
    }

    //启动导航App
    private void startNavApp(LatLng latLng) {
        try {
            String location = latLng.latitude + "," + latLng.longitude;
            Intent nav = new Intent();
            nav.setData(Uri.parse("baidumap://map/navi?location=" + location));
            mContext.startActivity(nav);
        } catch (ActivityNotFoundException e) {//未安装百度地图
            try {
                Intent gaode = new Intent("android.intent.action.VIEW",
                        Uri.parse("androidamap://navi?sourceApplication=快处快赔&lat=" +
                                latLng.latitude + "&lon=" + latLng.longitude + "&dev=0"));
                gaode.setPackage("com.autonavi.minimap");
                mContext.startActivity(gaode);
            } catch (Exception ex) {//未安装高德地图
                UiUtil.showToast(mContext, "未安装地图客户端，无法为您导航");
            }
        }
    }

    private PromptDialog dialog;

    private void showReportDialog() {
        if (null == dialog) {
            String title = mContext.getString(R.string.insurance_wait);
            String content = mContext.getString(R.string.nav_content);
            dialog = new PromptDialog(mContext);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setConfirm("确定", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    sendMsg();
                }
            });
            dialog.setCancel("取消", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.setTitleNoIcon(title);
            dialog.setInfo(content);
        }
        dialog.show();
    }

    //发送短信提示给服务中心
    private void sendMsg() {
        BaseApplication mApplication = ((MainActivity) mContext).getAppData();
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
        String centerid = dataBean.getString("centerid");
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(mContext).sendServiceCenter(appcaseno, centerid, uid, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                if (mView == null) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    String longitude = dataBean.getString("gisp_y");
                    String latitude = dataBean.getString("gisp_x");
                    if (TextUtils.isEmpty(longitude) || TextUtils.isEmpty(latitude)) {
                        UiUtil.showToast(mContext, "地址有误！");
                        return;
                    }
                    LatLng latLng = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
                    startNavApp(latLng);

                    backBtn.setVisibility(View.VISIBLE);
                    return;
                }
                String redes = bean.getString("redes");
                if (!TextUtils.isEmpty(redes)) {
                    UiUtil.showToast(mContext, redes);
                }
            }
        });
    }

    //服务中心关联到案件编号
    private void bindServiceCenter() {
        BaseApplication mApplication = ((MainActivity) mContext).getAppData();
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
        String centerid = dataBean.getString("centerid");
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(mContext).bindServiceCenter(appcaseno, centerid, uid, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                if (mView == null) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    UiUtil.showToast(mContext, "完成成功！");
                    if (null != infoBeans) {
                        IntentInsuranceReportView view = new IntentInsuranceReportView
                                (mContext, infoBeans, appcaseno, isSingle);
                        view.setFinish(true);
                        FrameworkManager.getInstance().showNewForm(mContext, view);
                    } else {
                        LSMsgCall(24, "finishservice");
                        OnDestroy();
                        closeForm();
                    }
                    return;
                }
                String redes = bean.getString("redes");
                if (!TextUtils.isEmpty(redes)) {
                    UiUtil.showToast(mContext, redes);
                }
            }
        });
    }

    @Override
    public void refresh() {

    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        setEvent(false);
        mView = null;
    }
}
